package net.zhengxinyun.performance.service.warn;

import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.entity.WarnEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.SixIndicatorsFilter;
import net.zhengxinyun.performance.filter.TownIndicatorsFilter;
import net.zhengxinyun.performance.filter.WarnFilter;
import net.zhengxinyun.performance.mapper.Indicators.CountryIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.SixIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.check.CheckEntityMapper;
import net.zhengxinyun.performance.mapper.warn.WarnEntityMapper;
import net.zhengxinyun.performance.service.indicators.CountryIndicatorsService;
import net.zhengxinyun.performance.service.indicators.SixIndicatorsService;
import net.zhengxinyun.performance.service.indicators.TownIndicatorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyp1202 on 2019/2/18.
 */
@Service("WarnService")
@Transactional
public class WarnServiceImpl implements WarnService {

    private WarnEntityMapper warnEntityMapper;
    private TownIndicatorsEntityMapper townIndicatorsEntityMapper;
    private SixIndicatorsEntityMapper sixIndicatorsEntityMapper;
    private CountryIndicatorsEntityMapper countryIndicatorsEntityMapper;
    private CheckEntityMapper checkEntityMapper;
    private CountryIndicatorsService countryIndicatorsService;
    private TownIndicatorsService townIndicatorsService;
    private SixIndicatorsService sixIndicatorsService;

    @Autowired
    public WarnServiceImpl(WarnEntityMapper warnEntityMapper, TownIndicatorsEntityMapper townIndicatorsEntityMapper, SixIndicatorsEntityMapper sixIndicatorsEntityMapper, CountryIndicatorsEntityMapper countryIndicatorsEntityMapper, CheckEntityMapper checkEntityMapper, CountryIndicatorsService countryIndicatorsService, TownIndicatorsService townIndicatorsService, SixIndicatorsService sixIndicatorsService) {
        this.warnEntityMapper = warnEntityMapper;
        this.townIndicatorsEntityMapper = townIndicatorsEntityMapper;
        this.sixIndicatorsEntityMapper = sixIndicatorsEntityMapper;
        this.countryIndicatorsEntityMapper = countryIndicatorsEntityMapper;
        this.checkEntityMapper = checkEntityMapper;
        this.countryIndicatorsService = countryIndicatorsService;
        this.townIndicatorsService = townIndicatorsService;
        this.sixIndicatorsService = sixIndicatorsService;
    }

    @Override
    @Scheduled(cron = "* * 0 * * ?")
    public void insert() {
        System.out.println("================预警方法开始=====================");
        String today = DateUtils.getDate();
        String dateTime = DateUtils.getYear();
        List<CountryIndicatorsFilter> countryIndicatorsFilterList = countryIndicatorsService.queryWarn(today, dateTime);
        List<TownIndicatorsFilter> townIndicatorsFilterList = townIndicatorsService.queryWarn(today, dateTime);
        List<SixIndicatorsFilter> sixIndicatorsFilters = sixIndicatorsService.queryWarn(today, dateTime);
        try {
            for (CountryIndicatorsFilter country : countryIndicatorsFilterList) {
                int i = insertWarn(country);
                if (i == 0) {
                    throw new Exception();
                }
            }
            for (TownIndicatorsFilter town : townIndicatorsFilterList) {
                int i = insertWarn(town);
                if (i == 0) {
                    throw new Exception();
                }
            }
            for (SixIndicatorsFilter six : sixIndicatorsFilters) {
                int i = insertWarn(six);
                if (i == 0) {
                    throw new Exception();
                }
            }
        } catch (Exception e) {
            System.out.println("失败"+e);
        }
    }

    @Override
    public String update(WarnEntity warnEntity) {
        return null;
    }

    @Override
    public List<WarnFilter> query(WarnFilter warnFilter) {
        return warnEntityMapper.query(warnFilter);
    }

    @Override
    public List<WarnFilter> loginQuery(UserEntity userEntity) {
        WarnFilter warnFilter = new WarnFilter();
        warnFilter.setStatus("0");
        warnFilter.setUnit(userEntity.getUserDepartment());
        List<WarnFilter> list = warnEntityMapper.query(warnFilter);
        WarnEntity warnEntity = new WarnEntity();
        for (WarnFilter filter : list) {
            warnEntity.setId(filter.getId());
            warnEntity.setStatus("1");
            warnEntityMapper.updateByPrimaryKeySelective(warnEntity);
        }
        return list;
    }


    @Override
    public int insertWarn(CountryIndicatorsFilter countryIndicatorsFilter) throws Exception {
        String month = countryIndicatorsFilter.getEndTime().substring(5, 7);
        String date = countryIndicatorsFilter.getDateTime().substring(0, 4);
        WarnEntity warnEntity ;
        int i = 1;
        switch (countryIndicatorsFilter.getWarnType()) {
            case "0":
                if (StringUtils.isBlank(countryIndicatorsFilter.getScore()) || !month.equals(countryIndicatorsFilter.getMonthTime())) {
                    warnEntity =  newEntity(countryIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "1":
                if (StringUtils.isBlank(countryIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(countryIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (DateUtils.getDifferMonth(countryIndicatorsFilter.getMonthTime(), month) > 3) {
                    warnEntity =  newEntity(countryIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "2":
                if (StringUtils.isBlank(countryIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(countryIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (DateUtils.getDifferMonth(countryIndicatorsFilter.getMonthTime(), month) > 6) {
                    warnEntity =  newEntity(countryIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "3":
                if (StringUtils.isBlank(countryIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(countryIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (date.equals(countryIndicatorsFilter.getDateTime())) {
                    warnEntity =  newEntity(countryIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            default:
                break;
        }
        return i;
    }

    @Override
    public int insertWarn(TownIndicatorsFilter townIndicatorsFilter) throws Exception {
        String month = townIndicatorsFilter.getEndTime().substring(5, 7);
        String date = townIndicatorsFilter.getDateTime().substring(0, 4);
        WarnEntity warnEntity ;
        int i = 1;
        switch (townIndicatorsFilter.getWarnType()) {
            case "0":
                if (StringUtils.isBlank(townIndicatorsFilter.getScore()) || !month.equals(townIndicatorsFilter.getMonthTime())) {
                    warnEntity =  newEntity(townIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "1":
                if (StringUtils.isBlank(townIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(townIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (DateUtils.getDifferMonth(townIndicatorsFilter.getMonthTime(), month) > 3) {
                    warnEntity =  newEntity(townIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "2":
                if (StringUtils.isBlank(townIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(townIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (DateUtils.getDifferMonth(townIndicatorsFilter.getMonthTime(), month) > 6) {
                    warnEntity =  newEntity(townIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "3":
                if (StringUtils.isBlank(townIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(townIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (date.equals(townIndicatorsFilter.getDateTime())) {
                    warnEntity =  newEntity(townIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            default:
                break;
        }
        return i;
    }

    @Override
    public int insertWarn(SixIndicatorsFilter sixIndicatorsFilter) throws Exception {
        String month = sixIndicatorsFilter.getEndTime().substring(5, 7);
        String date = sixIndicatorsFilter.getDateTime().substring(0, 4);
        WarnEntity warnEntity;
        int i = 1;
        switch (sixIndicatorsFilter.getWarnType()) {
            case "0":
                if (StringUtils.isBlank(sixIndicatorsFilter.getScore()) || !month.equals(sixIndicatorsFilter.getMonthTime())) {
                    warnEntity =  newEntity(sixIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "1":
                if (StringUtils.isBlank(sixIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(sixIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (DateUtils.getDifferMonth(sixIndicatorsFilter.getMonthTime(), month) > 3) {
                    warnEntity =  newEntity(sixIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "2":
                if (StringUtils.isBlank(sixIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(sixIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (DateUtils.getDifferMonth(sixIndicatorsFilter.getMonthTime(), month) > 6) {
                    warnEntity =  newEntity(sixIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            case "3":
                if (StringUtils.isBlank(sixIndicatorsFilter.getScore())) {
                    warnEntity =  newEntity(sixIndicatorsFilter);
                    i = replyInsert(warnEntity);
                } else if (date.equals(sixIndicatorsFilter.getDateTime())) {
                    warnEntity =  newEntity(sixIndicatorsFilter);
                    i = replyInsert(warnEntity);
                }
                break;
            default:
                break;
        }
        return i;
    }

    @Override
    public int replyInsert(WarnEntity warnEntity) {
        Integer i = warnEntityMapper.queryReply(warnEntity);
        if (i == 0) {
            int a = warnEntityMapper.insert(warnEntity);
            return a;
        } else {
            int b = warnEntityMapper.updateReply(warnEntity);
            return b;
        }
    }

    @Override
    public String updateStatusById(Long id) {
        WarnEntity entity = new WarnEntity();
        WarnFilter warnFilter = new WarnFilter();
        warnFilter.setIndexId(id);
        List<WarnFilter> list = warnEntityMapper.query(warnFilter);
        if (list == null || list.size() == 0){
            return "预警信息修改成功";
        }
        entity.setIndexId(id);
        entity.setStatus("2");
        int i = warnEntityMapper.updateByPrimaryKeySelective(entity);
        if (i == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "预警信息修改失败";
        }
        return "预警信息修改成功";
    }

    @Override
    public Map<String, Object> queryCountry(WarnFilter warnFilter) {
        List<WarnFilter> list ;
        list = warnEntityMapper.queryForCountry(warnFilter);
        if (list == null || list.size() == 0){
            warnFilter.setPageNumber(warnFilter.getPageNumber()-1);
            list = warnEntityMapper.queryForCountry(warnFilter);
        }
        Double pageTotal = Util.intGetTotalPage(warnEntityMapper.queryForCountryCount(warnFilter),warnFilter.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pageTotal",pageTotal);
        return map;
    }

    @Override
    public Map<String, Object> queryTown(WarnFilter warnFilter) {
        List<WarnFilter> list ;
        list = warnEntityMapper.queryForTown(warnFilter);
        if (list == null || list.size() == 0){
            warnFilter.setPageNumber(warnFilter.getPageNumber()-1);
            list = warnEntityMapper.queryForTown(warnFilter);
        }
        Double pageTotal = Util.intGetTotalPage(warnEntityMapper.queryForTownCount(warnFilter),warnFilter.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pageTotal",pageTotal);
        return map;
    }

    @Override
    public Map<String, Object> querySix(WarnFilter warnFilter) {
        List<WarnFilter> list ;
        list = warnEntityMapper.queryForSix(warnFilter);
        if (list == null || list.size() == 0){
            warnFilter.setPageNumber(warnFilter.getPageNumber()-1);
            list = warnEntityMapper.queryForSix(warnFilter);
        }
        Double pageTotal = Util.intGetTotalPage(warnEntityMapper.queryForSixCount(warnFilter),warnFilter.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("pageTotal",pageTotal);
        return map;
    }

    public WarnEntity newEntity(Object obj){
        String name = obj.getClass().getName();
        WarnEntity warnEntity = new WarnEntity();
        if ("net.zhengxinyun.performance.filter.CountryIndicatorsFilter".equals(name)){
            CountryIndicatorsFilter countryIndicatorsFilter = (CountryIndicatorsFilter) obj;
            warnEntity.setUnit(countryIndicatorsFilter.getMainUnit());
            warnEntity.setMessage("《"+countryIndicatorsFilter.getIndexName()+"》" + "分数请尽快录入");
            warnEntity.setIndexId(countryIndicatorsFilter.getId());
            warnEntity.setStatus("0");
            return warnEntity;
        }else if ("net.zhengxinyun.performance.filter.TownIndicatorsFilter".equals(name)){
            TownIndicatorsFilter townIndicatorsFilter = (TownIndicatorsFilter) obj;
            warnEntity.setUnit(townIndicatorsFilter.getMainUnit());
            warnEntity.setMessage("《"+townIndicatorsFilter.getIndexName()+"》" + "分数请尽快录入");
            warnEntity.setIndexId(townIndicatorsFilter.getId());
            warnEntity.setStatus("0");
            return warnEntity;
        }else if ("net.zhengxinyun.performance.filter.SixIndicatorsFilter".equals(name)){
            SixIndicatorsFilter sixIndicatorsFilter = (SixIndicatorsFilter) obj;
            warnEntity.setUnit(sixIndicatorsFilter.getMainUnit());
            warnEntity.setMessage("《"+sixIndicatorsFilter.getIndexName()+"》" + "分数请尽快录入");
            warnEntity.setIndexId(sixIndicatorsFilter.getId());
            warnEntity.setStatus("0");
            return warnEntity;
        }else {
            return null;
        }
    }

}
